package com.sakiko.audiobook.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakiko.audiobook.common.login.SakiLogin;
import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.common.util.AuthContextHolder;
import com.sakiko.audiobook.model.order.OrderInfo;
import com.sakiko.audiobook.order.service.OrderInfoService;
import com.sakiko.audiobook.vo.order.OrderInfoVo;
import com.sakiko.audiobook.vo.order.TradeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 根据状态查看用户订单列表
     *
     * @param pageNo
     * @param pageSize
     * @param request
     * @return
     */
    @SakiLogin
    @Operation(summary = "根据状态查看用户订单列表")
    @GetMapping("/findUserPage/{pageNo}/{pageSize}") // ?orderStatus => RequestParam(required=false) orderStatus..
    public Result findUserPage(@PathVariable("pageNo") Long pageNo,
                               @PathVariable("pageSize") Long pageSize,
                               HttpServletRequest request) {
        // 获取订单状态 (主要是 "我的 -> 全部-已支付-..." )
        String orderStatus = request.getParameter("orderStatus");
        // 获取用户 Id
        Long userId = AuthContextHolder.getUserId();
        // 构建分页条件
        Page<OrderInfo> orderInfoPage = new Page<>(pageNo, pageSize);
        // 查询结果并返回
        IPage<OrderInfo> iPage = orderInfoService.findUserPage(orderInfoPage, orderStatus, userId);
        return Result.ok(iPage);
    }

    /**
     * 根据订单编号获取订单详情
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单编号获取订单详情")
    @GetMapping("/getOrderInfo/{orderNo}")
    public Result getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        return Result.ok(orderInfo);
    }

    /**
     * 立即结算
     *
     * @param orderInfoVo
     * @return
     */
    @SakiLogin
    @Operation(summary = "立即结算")
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        String orderNo = orderInfoService.submitOrder(orderInfoVo, AuthContextHolder.getUserId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return Result.ok(map);
    }

    /**
     * 订单支付结算页
     *
     * @param tradeVo
     * @return
     */
    @SakiLogin
    @Operation(summary = "订单支付结算页")
    @PostMapping("/trade")
    public Result trade(@RequestBody TradeVo tradeVo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo, userId);
        return Result.ok(orderInfoVo);
    }

}

/*
关于订单提交:
1、余额支付:
    a、扣减余额
    b、保存订单
    c、保存用户购买记录 (专辑详情列表 回显要 user_paid_track or album)
2、在线支付:
    a、保存订单信息
    b、发送延迟消息，实现取消订单功能
关于多线程编程:
1、创建资源类、属性和操作方法
2、在资源类的操作方法中进行: 判断、干活儿、通知
3、创建多线程调用资源类的方法
4、防止虚假唤醒现象
 */
